package com.zhongyouyun.utils;

import com.zhongyouyun.other.model.Result;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;


@Data
@Builder
public class ResultUtils {
    public <T> Result<T> getFailResult(BindingResult bindingResult, Result<T> result) {

        result.setStatus(Result.fail);
        List<ObjectError> errorList = bindingResult.getAllErrors();
        StringBuilder messages = new StringBuilder();
        errorList.forEach(error -> {
            messages.append(error.getDefaultMessage()).append(";");
        });
        result.setMessage(messages.toString());
        return result;
    }
}
